﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Zor_Modes;
using Zor_IDAL;
using Zor_Tools;
using System.Data;
using Dapper;

namespace Zor_DAL
{
  public  class Zor_Examine_DAL<T> where T : class
    {


      /// <summary>
      /// 根据审核ID 获取审核信息
      /// </summary>
      /// <param name="Examine_Id"></param>
      /// <returns></returns>

      public static List<T> getExamineInfo(int Examine_Id)
      {
          string sql = "select * from V_Zor_Examine where Examine_Id =" + Examine_Id + " ";
          List<T> lst_ExamineInfo = DapperQuery<T>.DoQueryList(sql, null);
          return lst_ExamineInfo;
      }
      
      
      /// <summary>
        /// 审核
        /// create zhl by 17.3.31
        /// </summary>
        /// <param name="examine">审核对象</param>
        /// <param name="rentMoney">评定租金</param>
        /// <returns>2--当前评议已推送-1操作失败1--操作成功的标识</returns>
      public static BaseAnswer Examine(Zor_Examine examine, string accpid, double rentMoney)
        {
            List<string> lstsql = new List<string>();
            if (rentMoney > 31500 )
            {

                //更新审核状态审核时间，审核人，审核意见
                lstsql.Add(string.Format("update [Zor_Examine] set Examine_State=1,Examine_Date=" + TimeHelper.ToTimeStamp(DateTime.Now) + ",Examine_UserId=" + RequestSession.GetSessionUser().User_Id + ",Examine_Comment='" + examine.Examine_Comment + "' where Examine_Id=" + examine.Examine_Id + ""));
                //更新受理单状态
                lstsql.Add(string.Format("update [Zor_Acceptance] set Acceptance_State=5 where Acceptance_Id=" + accpid + ""));
                //金山表增加数据
                lstsql.Add(string.Format("insert into Zor_Jinsan(Examine_Id,Js_State) values (" + examine.Examine_Id + ",1)"));
            }
            else
            {
                //更新审核状态审核时间，审核人，审核意见
                lstsql.Add(string.Format("update [Zor_Examine] set Examine_State=1,Examine_Date=" + TimeHelper.ToTimeStamp(DateTime.Now) + ",Examine_UserId=" + RequestSession.GetSessionUser().User_Id + ",Examine_Comment='" + examine.Examine_Comment + "' where Examine_Id=" + examine.Examine_Id + ""));
                //更新受理单状态
                lstsql.Add(string.Format("update [Zor_Acceptance] set Acceptance_State=5 where Acceptance_Id=" + accpid + ""));
                //缴税表增加数据
                lstsql.Add(string.Format("insert into Zor_PayTaxes(Examine_Id,Pt_State,User_Id,Pt_CreateDate) values (" + examine.Examine_Id + ",0," + RequestSession.GetSessionUser().User_Id + "," + TimeHelper.ToTimeStamp(DateTime.Now) + ")"));
            }
         
            BaseAnswer bar = DapperTransaction.DoTransaction(lstsql);
            return bar;
        }

        /// <summary>
        /// 退回至评定
        /// create zhl by 17.3.31
        /// 评定状态(0正在评定,1已评定,2退回评定）
        /// </summary>
        /// <param name="aptid">受理单id</param>
        /// <returns></returns>
         public static BaseAnswer ReturnEvaluate(Zor_Examine examine, string accpid)
        {

            List<string> lstsql = new List<string>();
            //更新审核状态
            lstsql.Add(string.Format("update [Zor_Examine] set Examine_State=2 where Examine_Id=" + examine.Examine_Id + ""));

            //更新评定表状态
            lstsql.Add(string.Format("update [Zor_Evaluate] set Evaluate_State=2, Evaluate_Remarks='" + examine.Examine_Comment + "' where Evaluate_Id=" + examine.Evaluate_Id + ""));
          
            //更新受理单表
            lstsql.Add(string.Format("update [Zor_Acceptance] set Acceptance_State=3 where Acceptance_Id=" + accpid + ""));
            //退回记录表增加数据
            lstsql.Add(string.Format("insert into Zor_ReturnRecord values(" + examine.Examine_Id + ",'" + examine.Examine_Comment + "'," + RequestSession.GetSessionUser().User_Id + "," + TimeHelper.ToTimeStamp(DateTime.Now) + ",3)"));
            BaseAnswer bar = DapperTransaction.DoTransaction(lstsql);
            return bar;
        }
    }
}
